// pages/evaluation/evaluationtestResult/evaluationtestResult.js
const app = getApp()
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    GetTestresultInquire, //HLD测评结果查询
    GetTestmbtiList, // MBTI测评结果查询
    GetTestresultInquireCharacter, // 测评结果查询性格
} from '../../../api/index'
var radarChart = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        progressWidth: 80, // 初始进度条宽度为50%
        model: null,
        unscrambleArray: [],
        professionArray: [], // 专业
        jobArrayArray: [], // 职业
        radarData: [],
        typeArray:[],
        indicator:[],
        types:'',
    },
    animateProgressBar: function () {
        const animation = wx.createAnimation({
          duration: 2000, // 动画持续时间，单位ms
          timingFunction: 'linear'
        });
        animation.width('100%').step(); // 将进度条宽度扩展到100%
        this.setData({
          progressAnimation: animation.export()
        });
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.animateProgressBar();
        let openid = wx.getStorageSync('openId') || '';
        console.log(options)
        this.setData({
            types:options.types
        })
        if (options.types == 'MBTI') {
            this._GetTestmbtiList('?openId=' + openid, true)
        } else {
            this._GetTestresultInquire('?openId=' + openid, true)
        }
        this._GetTestresultInquireCharacter('?pageNum=1&pageSize=10', false)
    },
  
   
    //  测评结果查询
    _GetTestresultInquire(data, isLoading) {
        GetTestresultInquire(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                let array = res.rows;
                if (array.length != 0) {
                    let model = res.rows[0];
                    let typeOne = model.testResult.slice(0,1);
                    let typeTwo = model.testResult.slice(1,2);
                    let typeThree = model.testResult.slice(2,3);
                    var titleOne = model.testResult.slice(0,1);
                    var titleTwo = model.testResult.slice(1,2);
                    var titleThree = model.testResult.slice(2,3);
                    let array = [{type:"R",title:'实际型'},{type:"A",title:'艺术型'},{type:"I",title:'调研型'},{type:"S",title:'社会型'},{type:"E",title:'企业型'},{type:"C",title:'常规型'}]
                    array.forEach((item,index,array)=>{
                        if (item.type == typeOne) {
                            titleOne = item.title
                        } else if (item.type == typeTwo) {
                            titleTwo = item.title
                        } else if (item.type == typeThree) {
                            titleThree = item.title
                        }
                    })
                    this.setData({
                        model: model,
                        professionArray: model.itemMajor.split("、"),
                        jobArrayArray: model.itemJob.split("、"),
                        radarData: [model.scoreR,model.scoreC,model.scoreA,model.scoreI,model.scoreS,model.scoreE],
                        typeArray:[{type:typeOne,title:titleOne},{type:typeTwo,title:titleTwo},{type:typeThree,title:titleThree}],
                        indicator: [{
                            name: `实际型(${model.scoreR})`,
                            max: 20,
                          },
                          {
                            name: `常规型(${model.scoreC})`,
                            max: 20,
                          },
                          {
                            name: `艺术型(${model.scoreA})`,
                            max: 20,
                          },
                          {
                            name: `调研型(${model.scoreI})`,
                            max: 20,
                          },
                          {
                            name: `社会型(${model.scoreS})`,
                            max: 20,
                          },
                          {
                            name: `企业型(${model.scoreE})`,
                            max: 20,
                          }
                          ]
                    })
                }

            } else {
                Toast(res.msg)
            }
        })
    },

    //  测评结果查询MBTI
    _GetTestmbtiList(data, isLoading) {
        GetTestmbtiList(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                let array = res.rows;
 
                if (array.length != 0) {
                    let model = res.rows[0];

                    let charToRemove = ',';
                    let modifiedTestResult = model.testResult.split(charToRemove).join('');

                    let typeOne = modifiedTestResult.slice(0,1);
                    let typeTwo = modifiedTestResult.slice(1,2);
                    let typeThree = modifiedTestResult.slice(2,3);
                    let typeFour = modifiedTestResult.slice(3,4);
                    var titleOne = modifiedTestResult.slice(0,1);
                    var titleTwo = modifiedTestResult.slice(1,2);
                    var titleThree = modifiedTestResult.slice(2,3);
                    var titleFour = modifiedTestResult.slice(3,4);
                    let array = [{type:"E",title:'向外'},{type:"I",title:'向内'},{type:"S",title:'实感'},{type:"N",title:'直觉'},{type:"T",title:'理性'},{type:"F",title:'情感'},{type:"J",title:'趋定'},{type:"P",title:'顺变'}]
                    array.forEach((item,index,array)=>{
                        if (item.type == typeOne) {
                            titleOne = item.title
                        } else if (item.type == typeTwo) {
                            titleTwo = item.title
                        } else if (item.type == typeThree) {
                            titleThree = item.title
                        }  else if (item.type == typeFour) {
                            titleFour = item.title
                        }
                    })
                    this.setData({
                        model: model,
                        ['model.testResult']: modifiedTestResult,
                        typeArray:[{type:typeOne,title:titleOne},{type:typeTwo,title:titleTwo},{type:typeThree,title:titleThree},{type:typeFour,title:titleFour}],
                    })
                }
            } else {
                Toast(res.msg)
            }
        })
    },

    //  测评结果查询性格
    _GetTestresultInquireCharacter(data, isLoading) {
        GetTestresultInquireCharacter(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                this.setData({
                    unscrambleArray: res.rows
                })
            } else {
                Toast(res.msg)
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
    
})